#include <ESP32Servo.h>
#include <DHT.h>
#include "ycwl.h"
#include "SSD1306Wire.h"
#include <Arduino.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
DHT dht(DHT11_PIN, DHTTYPE);
SSD1306Wire display(0x3c, 4, 14);

/**
 **oled function
 **/
// oled initialization
void oled_init()
{
  display.init();
  display.flipScreenVertically();
}
void oled_clear()
{
  display.clear();
}
void oled_display()
{
  display.display();
}
void oled_drawString(int16_t x, int16_t y, String text)
{
  display.drawString(x, y, text);
}
// 可选字符 ArialMT_Plain_10 ArialMT_Plain_16 ArialMT_Plain_24
void oled_setfont(uint8_t num)
{
  if (num == 1)
    display.setFont(ArialMT_Plain_10);
  else if (num == 2)
    display.setFont(ArialMT_Plain_16);
  else if (num == 3)
    display.setFont(ArialMT_Plain_24);
}
void oled_drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress)
{
  display.drawProgressBar(x, y, width, height, progress);
}
void oled_fillRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress)
{
  display.fillRect(x, y, width, height);
}
void oled_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1)
{
  display.drawLine(x0, y0, x1, y1);
}
void oled_drawRect(int16_t x, int16_t y, int16_t width, int16_t height)
{
  display.drawRect(x, y, width, height);
}
void oled_drawHorizontalLine(int16_t x, int16_t y, int16_t length)
{
  display.drawHorizontalLine(x, y, length);
}
void oled_drawCircle(int16_t x, int16_t y, int16_t radius)
{
  display.drawCircle(x, y, radius);
}
void oled_fillCircle(int16_t x, int16_t y, int16_t radius)
{
  display.fillCircle(x, y, radius);
}
void oled_setPixel(int16_t x, int16_t y)
{
  display.setPixel(x, y);
}
void oled_invertDisplay()
{
  display.invertDisplay();
}

/**
 **led function
 **/
// led initialization
void led_init()
{
  pinMode(LED, OUTPUT);
}
// led open
void led_on()
{
  digitalWrite(LED, HIGH);
}
// led open
void led_off()
{
  digitalWrite(LED, LOW);
}

/**
 **beep function
 **/
// beep initialization
void beep_init()
{
  pinMode(BEEP, OUTPUT);
}
// beep open
void beep_on()
{
  digitalWrite(BEEP, HIGH);
}
// beep close
void beep_off()
{
  digitalWrite(BEEP, LOW);
}

/**
 **relay function
 **/
// relay initialization
void relay_init()
{
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
}
// relay open
void relay_on(uint pin)
{
  if (pin == 1)
    digitalWrite(RELAY1, LOW);
  else if (pin == 2)
    digitalWrite(RELAY2, LOW);
}
// relay close
void relay_off(uint pin)
{
  if (pin == 1)
    digitalWrite(RELAY1, HIGH);
  else if (pin == 2)
    digitalWrite(RELAY2, HIGH);
}

/**
 **servo function
 **/
// servo initialization
void servo_init(uint8_t pin)
{
  ESP32PWM::allocateTimer(0);

  if (pin == 1)
  {
    myservo1.setPeriodHertz(50);        // standard 50 hz servo
    myservo1.attach(SERVO1, 500, 2500); // attaches the servo on pin 18 to the servo object
  }
  else if (pin == 2)
  {
    myservo2.setPeriodHertz(50);        // standard 50 hz servo
    myservo2.attach(SERVO2, 500, 2500); // attaches the servo on pin 18 to the servo object
  }
}
// servo control
void servo_pos(int pin, int pos)
{
  if (pin == 1)
    myservo1.write(pos);
  else if (pin == 2)
    myservo2.write(pos);
}
/**
 **key function
 **/
// key initialization
void key_init()
{
  pinMode(KEY, INPUT_PULLUP);
}
// 读取机械按键
uint8_t key_read()
{
  int val = analogRead(KEY);
  if (val == 0)
  {
    delay(20);
    if (analogRead(KEY) == 0){
      return 1;
    }
    // int a = 0;
    // while (analogRead(KEY) < 100 && a<50){
    //   a++;
    //   delay(10);
    // }
  }
  else
  {
    return 0;
  }
}
// 读取电容按键
uint8_t key_read_touch()
{
  int val = analogRead(KEY);
  if (val < 100)
  {
    // int a = 0;
    // while (analogRead(KEY) < 100 && a<50){
    //   a++;
    //   delay(10);
    // }
    // delay(50);
    return 1;
  }
  else
  {
    return 0;
  }
}
/**
 **dht11 function
 **/
void dht11_init()
{
  dht.begin();
}

float dht11_humidity()
{
  return dht.readHumidity();
}
float dht11_temperature()
{
  return dht.readTemperature();
}
/**
 **rain check function
 **/
// rain check initialization
void rain_init()
{
  pinMode(RAIN_AO, INPUT);
  pinMode(RAIN_DO, INPUT);
}
int16_t rain_read_ao()
{
  return analogRead(RAIN_AO);
}
int8_t rain_read_do()
{
  return !digitalRead(RAIN_DO);
}
/**
 **fire check function
 **/
// fire check initialization
void fire_init()
{
  pinMode(FIRE_AO, INPUT);
  pinMode(FIRE_DO, INPUT);
}
int16_t fire_read_ao()
{
  return analogRead(FIRE_AO);
}
int8_t fire_read_do()
{
  return digitalRead(FIRE_DO);
}

/**
 **smog check function
 **/
// smog check initialization
void smog_init()
{
  pinMode(SMOG_AO, INPUT);
  pinMode(SMOG_DO, INPUT);
}
int16_t smog_read_ao()
{
  return analogRead(SMOG_AO);
}
int8_t smog_read_do()
{
  return digitalRead(SMOG_DO);
}

/**
 **sound check function
 **/
// sound check initialization
void sound_init()
{
  pinMode(SOUND_AO, INPUT);
  pinMode(SOUND_DO, INPUT);
}
int16_t sound_read_ao()
{
  return analogRead(SOUND_AO);
}
int8_t sound_read_do()
{
  return digitalRead(SOUND_DO);
}

/**
 **body check function
 **/
// body check initialization
void body_init()
{
  pinMode(BODY, INPUT);
}

int8_t body_read()
{
  return digitalRead(BODY);
}